package org.springblade.common.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * Druid filter config, add DruidFilter to filter chains
 * @author Sharry
 * @since 2023/7/20
 */
@Configuration
public class DruidFilterConfig {

	@Bean
	public DruidFilter druidFilter() {
		return new DruidFilter();
	}

	@Bean
	@ConditionalOnProperty(prefix = "filter.druidFilter", name = "enabled", havingValue = "true")
	public FilterRegistrationBean<Filter> druidFilterRegistration() {
		FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
		registration.setFilter(druidFilter());
		registration.addUrlPatterns("/*");
		registration.setName("DruidFilter");
		// order of filter
		registration.setOrder(1);
		return registration;
	}


}

